<?php


include("config.php");

$erreur = 0;
$form = 1;
$proceed = 1;


	//On verifie si l'on a un premier acces à la page
	if(isset($_POST['pseudo']))
	{
		//On verifie que tous les champs soient remplis
		if(empty($_POST['pseudo']) || empty($_POST['password']) || empty($_POST['firstname'] )
				|| empty($_POST['lastname']) || empty($_POST['email']) )
		{
			$erreur = 1;
			echo "Empty fields are not allowed <br>";
		}
		else
		{	
			//On affecte la valeur des champs à nos variables
			$pseudo = $_POST['pseudo'];
			$password = sha1($_POST['password']);
			$firstname = $_POST['firstname'];
			$lastname = $_POST['lastname'];
			$email = $_POST['email'];


			if (!preg_match("#.+@.+\..+#", $email))
			{
				echo "Email provided appears to be wrong<br>";
				$erreur = 1;
			}


			if(!$erreur)
			{
				//On se connecte à la base de donnée
				$link = mysql_connect ('localhost', DB_USER_NAME, DB_PASSWORD) or die ("Cannot join database");
				mysql_select_db (DB_NAME);

				//On verifie si le pseudo n'existe pas déjà
				$requete = "SELECT * FROM user WHERE pseudo='" . $pseudo . "'";
				$result = mysql_query ($requete);
	
				if(mysql_num_rows($result) == 0)
				{
					// Upload des images

					
						$dossier = 'avatar/';
						$fichier = basename($_FILES['avatar']['name']);
						$taille_maxi = 100000;
						$taille = filesize($_FILES['avatar']['tmp_name']);
						$extensions = array('.jpg', '.jpeg');
						$extension = strrchr($_FILES['avatar']['name'], '.'); 

						//Début des vérifications de sécurité...
						if(!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau
						{
							$error = 'Please, upload jpeg file<br>';
						}
						if($taille>$taille_maxi)
						{
							$error = 'File is to big<br>';
							
						}
						if(!isset($error)) //S'il n'y a pas d'erreur, on upload
						{
							//On formate le nom du fichier ici...
							$fichier = $_POST['pseudo'] . ".jpg";
							if(move_uploaded_file($_FILES['avatar']['tmp_name'], $dossier . $fichier)) //Si la fonction renvoie TRUE, c'est que ça a fonctionné...
							{
								
							}
							else //Sinon (la fonction renvoie FALSE).
							{
								echo 'Upload failed !<br>';
								$proceed = 0;
								$erreur = 1;
							}
						}
						else
						{
							echo $error;
							$proceed = 0;
							$erreur = 1;
						}
					

					if($proceed)
					{
						// Insertion de l'utilisateur dans la BDD
						$requete = "INSERT INTO user(id_user,pseudo,password,nom,prenom,email,inscription) 
									VALUES ('', '" . $pseudo . "', '" . $password . "', '" . $lastname . "',
									'" . $firstname . "', '" . $email . "', '" . time() . "')";

						echo "Subscription succeed <br>";
						$form = 0;

					}
					mysql_query ($requete);

					

				}
				else
				{
					echo "User name \"" . $pseudo . "\" already used<br>";
					$erreur = 1;
				}

				mysql_close($link);
			}
		}
	}


		if($form)
		{
			echo "Provide some informations to proceed your subscription:</br>	";
			if($erreur)
			{
			?>
				<form method="post" action="index.php"  enctype="multipart/form-data">
				<p><input type="text" placeholder="Pseudo" name="pseudo" value="<? echo $_POST['pseudo']?>"/></p>
				<p><input type="password" placeholder="Password" name="password"/></p>
				<p><input type="text" placeholder="First name" name="firstname" value="<? echo $_POST['firstname']?>"/></p>
				<p><input type="text" placeholder="Last name" name="lastname" value="<? echo $_POST['lastname']?>"/></p>
				<p><input type="text" placeholder="Email" name="email" value="<? echo $_POST['email']?>"/></p>
				<p>Avatar :</p>
				<p><input type="file" placeholder="Avatar" name="avatar"></p>
				<input type="hidden" name="MAX_FILE_SIZE" value="100000">
				<p><input type="submit" value="Valider" /></p>
				</form>
			<?php
			}
			else
			{
			?>
				<form method="post" action="index.php"  enctype="multipart/form-data">
				<p><input type="text" placeholder="Pseudo" name="pseudo" /></p>
				<p><input type="password" placeholder="Password" name="password" /></p>
				<p><input type="text" placeholder="First name" name="firstname" /></p>
				<p><input type="text" placeholder="Last name" name="lastname" /></p>
				<p><input type="text" placeholder="Email" name="email" /></p>
				<p>Avatar :</p>
				<p><input type="file" placeholder="Avatar" name="avatar"></p>
				<input type="hidden" name="MAX_FILE_SIZE" value="100000">
				<p><input type="submit" value="Valider" /></p>
				</form>
			<?php
			}
		}
?>

